<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ProjectExportRecord;
use App\Models\AdminUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProjectExportRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProjectExportRecord(), function (Grid $grid) {

            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
            // $grid->disableDeleteButton();
            $grid->disablePerPages();
            $grid->paginate(10);


            if (!Admin::user()->can('batch-delete-export-record')) {
                $grid->disableRowSelector();
                $grid->disableBatchDelete();
            }


            $grid->scrollbarX();
            $grid->withBorder();
            $grid->addTableClass(['table-text-center']);

            $grid->column('id')->sortable();
            $grid->column('file_name');
            // $grid->column('project_id');
            $grid->column('project_code');
            $grid->column('admin_id', '导出人')->display(function ($admin_id) {
                return AdminUser::find($admin_id)->name;
            });
            $grid->column('roles')->display(function ($roles) {
                $roles = json_decode($roles, true);
                // dd($roles);
                return $roles[0]['name'];
            });
            $grid->column('打款失败/打款总数')->display(function () {
                return "<span class='color:red'>$this->pay_fail/$this->pay_total</span>";
            });
            // $grid->column('pay_fail');
            // $grid->column('pay_success');
            // $grid->column('pay_total');
            $grid->column('created_at', '导出时间')->sortable();
            // $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('created_at', 'desc');

            // 管理员和创建人可以设置分配
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $currLoginUser = auth('admin')->user();

                // if (Admin::user()->isAdministrator() || Admin::user()->isRole('finance')) {

                    // append一个操作
                    $actions->prepend('<a href="/records/detail?records_id=' . $actions->row->id . '" class="text-custom" style="margin-right: 10px;"><i class="fa  fa-ellipsis-h"></i>明细</a>');
                // }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('file_name');
                $filter->date('created_at', '导出时间');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProjectExportRecord(), function (Show $show) {
            $show->field('id');
            $show->field('file_name');
            $show->field('project_id');
            $show->field('project_code');
            $show->field('admin_id');
            $show->field('role_id');
            $show->field('pay_fail');
            $show->field('pay_success');
            $show->field('pay_total');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProjectExportRecord(), function (Form $form) {
            $form->display('id');
            $form->text('file_name');
            $form->text('project_id');
            $form->text('project_code');
            $form->text('admin_id');
            $form->text('role_id');
            $form->text('pay_fail');
            $form->text('pay_success');
            $form->text('pay_total');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
